package com.example.wananrdroid.fragment.zhufragment;


import android.content.Intent;
import android.view.View;
import android.widget.LinearLayout;

import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.wananrdroid.Main2Activity;
import com.example.wananrdroid.R;
import com.example.wananrdroid.adapter.HomeAdapter;
import com.example.wananrdroid.base.BaseFragment;
import com.example.wananrdroid.bean.ArticleBean;
import com.example.wananrdroid.bean.BannerBean;
import com.example.wananrdroid.presenter.HomePresenter;
import com.example.wananrdroid.view.HomeView;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends BaseFragment<HomePresenter> implements HomeView {

    @BindView(R.id.home_toolbar)
     Toolbar mHomeToolbar;
    @BindView(R.id.home_re)
     RecyclerView mHomeRe;
    private ArrayList<ArticleBean.DataBean.DatasBean> datasBeans;
    private ArrayList<BannerBean.DataBean> bannerBeans;
    private HomeAdapter homeAdapter;

    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {
        mHomeToolbar.setTitle("");
        mHomeRe.setLayoutManager(new LinearLayoutManager(getContext()));
        mHomeRe.addItemDecoration(new DividerItemDecoration(getContext(), LinearLayout.VERTICAL));
        datasBeans = new ArrayList<>();
        bannerBeans = new ArrayList<>();
        homeAdapter = new HomeAdapter(getContext(), datasBeans, bannerBeans);
        mHomeRe.setAdapter(homeAdapter);
        homeAdapter.setOnContextClickListener(new HomeAdapter.OnContextClickListener() {
            @Override
            public void oncontextclicklistener(View v, int position) {
                if (position!=0){
                    String link = datasBeans.get(position).getLink();
                    Intent intent = new Intent(getContext(), Main2Activity.class);
                    intent.putExtra("url",link);
                    startActivity(intent);
                }
            }
        });
    }

    @Override
    protected void initPresenter() {
        mPresenter=new HomePresenter();
       mPresenter.getdata();
    }

    @Override
    protected int getLayout() {
        return R.layout.fragment_home;
    }

    @Override
    public void onSuccess(ArticleBean article) {
        datasBeans.addAll(article.getData().getDatas());
        homeAdapter.notifyDataSetChanged();
    }

    @Override
    public void onFali(String arterror) {

    }

    @Override
    public void onSuccess(BannerBean badata) {
        bannerBeans.addAll(badata.getData());
        homeAdapter.notifyDataSetChanged();
    }

    @Override
    public void onFali1(String badata) {

    }

    @Override
    public void showToast(String str) {

    }
}
